<?php

class Admin_PermissionController extends Zend_Controller_Action
{

    public function init()
    {
       $layoutPath = APPLICATION_PATH . '/templates/admin';
		$options = array (
				'layout' => 'index',
				'layoutPath' => $layoutPath 
		);
		Zend_Layout::startMvc ( $options );
    }

    public function indexAction()
    {
    	$Condition = array();
        $this->view->Permissions = Permission::getAllWithoutPager($Condition);
    }
	public function createAction(){
		$requests = $this->getRequest ()->getParams ();
		if ($this->getRequest ()->isPost ()) {
			try {
				$Permission = new Permission();
				$Permission->merge($requests);
				$Permission->module=$requests['modules'];
				$Permission->controller=$requests['controllers'];
				$Permission->action=$requests['actions'];
				$Permission->save();
				$this->view->Message="Create permission successfull";
			} catch (Exception $e) {
				print_r($e->getMessage());die();
			}
		}else{
			unset($requests['id']);
		}
	}
	public function editAction()
	{
		$requests = $this->getRequest()->getParams();
		$Permission = Permission::getById($requests['id']);
		if($Permission){
			$this->view->Permission = $Permission;
			if($this->getRequest()->isPost()){
				try {
					$Permission->merge($requests);
					$Permission->module=$requests['modules'];
					$Permission->controller=$requests['controllers'];
					$Permission->action=$requests['actions'];
					$Permission->save();
					$this->view->Message='Update permission successfull';
				} catch (Exception $e) {
					print_r($e->getMessage());die();
				}
			}
		}else{
			$this->view->Message='Exists isn\'t this permission';
		}
	}
	public function deleteAction()
	{
		$this->_helper->layout->disableLayout ();
		$this->_helper->viewRenderer->setNoRender (true);
		$json = array('STATUS'=>0);
		try {
			$requests = $this->getRequest()->getParams();
			$Permission =Permission::getById($requests['id']);
			$Permission->delete();
			$json['STATUS']=1;
		} catch (Exception $e) {
			$json['STATUS']=0;
		}
		header ( 'Content-type: application/json' );
		echo json_encode ( $json );
	}
	public function deletemultiAction()
	{
		$this->_helper->layout->disableLayout ();
		$this->_helper->viewRenderer->setNoRender (true);
		 
		try {
			$requests = $this->getRequest()->getParams();
			$arrID=$requests['permission'];
			foreach ($arrID as $key=>$value){
				$Permission=Permission::getById($value);
				if($Permission){
					$Permission->delete();
				}
			}
			$this->_redirect ( '/admin/permission/index');
		} catch (Exception $e) {
			print_r($e->getMessage());die();
		}
	}

}

